package com.znyj.servlet.task;

import com.znyj.servlet.task.model.ScheduledTaskModel;
import com.znyj.servlet.task.executor.ScheduledTaskExecutor;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by gaoliwei02 on 17/6/3.
 */
@Component
public class ScheduledTaskMapping extends AbstractTaskMapping<ScheduledTaskModel> {
    private static final Logger logger = Logger.getLogger(ScheduledTaskMapping.class);

    @Resource
    private ScheduledTaskExecutor scheduledTaskExecutor;

    //@Scheduled(cron = "0/2 * * * * ?")
    @Scheduled(fixedDelay = 10000)
    @Override
    public void sendTasks() {
        //把所有有任务设备的任务发送出去
        for (Map.Entry<Long, ScheduledTaskModel> entry : taskMap.entrySet()) {
            ScheduledTaskModel scheduledTaskModel = entry.getValue();
            long closeTime = scheduledTaskModel.scheduledEntity.getCreateTime().getTime() +
                    scheduledTaskModel.scheduledEntity.getDurationSec() * 1000 +
                    scheduledTaskModel.scheduledEntity.getExtraDelay() * 1000;
            if (closeTime < System.currentTimeMillis()) {
                scheduledTaskExecutor.send(scheduledTaskModel);
            }
        }
    }
}
